package com.songyuquan.webcs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songyuquan.webcs.pojo.Product;
import com.songyuquan.webcs.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * 后台商品数据控制器
 */
@Controller
@RequestMapping("/back")
public class BackController {

    @Autowired
    private ProductService productService;

    @GetMapping("/main")
    public String getBackPage() {
        return "back/backPage";
    }

    //分页显示商品列表
    @RequestMapping(value = "/product", method = RequestMethod.GET)
    public ModelAndView getBackProduct(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        ModelAndView modelAndView = new ModelAndView();
        Page<Product> productPage = new Page<>(pn, 10);
        Page<Product> page = productService.page(productPage, null);
        modelAndView.addObject("page", page);
        modelAndView.addObject("pn", pn);
        modelAndView.setViewName("back/backProduct");
        return modelAndView;
    }

    //高级查询 名称和类型
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    private ModelAndView getBackProduct(
            @RequestParam(value = "pn", defaultValue = "1") Integer pn,
            @RequestParam("name") String name,
            @RequestParam("categorylevelone_id") Integer categorylevelone_id)
    {
        ModelAndView modelAndView = new ModelAndView();
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        Page<Product> productPage = new Page<>(pn, 10);
        Page<Product> page;
        if (Objects.equals(name, "") && Objects.equals(categorylevelone_id, null)) {
            page = productService.page(productPage, null);
        } else if (Objects.equals(name, "")) {
            queryWrapper.eq("categorylevelone_id", categorylevelone_id);
            page = productService.page(productPage, queryWrapper);
        } else if (Objects.equals(categorylevelone_id, null)) {
            queryWrapper.like("name", name);
            page = productService.page(productPage, queryWrapper);
        } else {
            queryWrapper.like("name", name);
            queryWrapper.eq("categorylevelone_id", categorylevelone_id);
            page = productService.page(productPage, queryWrapper);
        }
        modelAndView.addObject("page", page);
        modelAndView.addObject("pn", pn);
        modelAndView.setViewName("back/backProduct");
        return modelAndView;
    }

    //跳转到添加商品页面
    @GetMapping("/addPage")
    public String getAddProduct() {
        return "back/addProduct";
    }

    //删除商品
    @GetMapping("/deleteProductById/{id}/{pn}")
    public String deleteProduct(@PathVariable("id") Integer id, @PathVariable Integer pn) {
        productService.removeById(id);
        return "redirect:/back/product?pn=" + pn;
    }

    //新增商品
    @PostMapping("/addProd")
    public String setProduct(
            @RequestParam("name") String name,
            @RequestParam("price") Float price,
            @RequestParam("integral") Integer integral,
            @RequestParam("stock") Integer stock,
            @RequestParam("categoryleveloneId") Integer categoryleveloneId,
            @RequestPart("productImg") MultipartFile productImg
    ) throws IOException {
        String fileName = null;
        if (!productImg.isEmpty()) {
            String originalFilename = UUID.randomUUID() + productImg.getOriginalFilename();
            productImg.transferTo(new File("D:\\thesis\\code\\webcs-syq\\src\\main\\resources\\static\\images\\" + originalFilename));
            fileName = originalFilename;
        }
        Product product = new Product();
        product.setName(name);
        product.setPrice(price);
        product.setIntegral(integral);
        product.setStock(stock);
        product.setCategoryleveloneId(categoryleveloneId);
        product.setFileName(fileName);
        productService.save(product);
        return "redirect:/back/product";
    }

    //获取修改商品数据
    @GetMapping("/updateProduct")
    public ModelAndView getUpdate(@RequestParam("productId") Integer productId, @RequestParam("pn") Integer pn) {
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.getById(productId);
        modelAndView.addObject("product", product);
        modelAndView.addObject("pn", pn);
        modelAndView.setViewName("back/updateProductPage");
        return modelAndView;
    }

    //修改商品
    @PostMapping("/updateById")
    public String updateById(
            @RequestParam("pn") Integer pn,
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("price") Float price,
            @RequestParam("fileName") String fileName,
            @RequestParam("integral") Integer integral,
            @RequestParam("stock") Integer stock,
            @RequestParam("categoryleveloneId") Integer categoryleveloneId,
            @RequestPart("productImg") MultipartFile productImg
    ) throws IOException {
        Product product = productService.getById(id);
        if (!name.isEmpty()) {
            product.setName(name);
        }
        if (price != null) {
            product.setPrice(price);
        }
        if (integral != null) {
            product.setIntegral(integral);
        }
        if (stock != null) {
            product.setStock(stock);
        }
        if (!Objects.equals(categoryleveloneId, product.getCategoryleveloneId())) {
            product.setCategoryleveloneId(categoryleveloneId);
        }
        String defaultFileName;
        if (!productImg.isEmpty()) {
            String originalFilename = UUID.randomUUID() + productImg.getOriginalFilename();
            productImg.transferTo(new File("D:\\thesis\\code\\webcs-syq\\src\\main\\resources\\static\\images\\" + originalFilename));
            defaultFileName = originalFilename;
        } else {
            defaultFileName = fileName;
        }
        product.setFileName(defaultFileName);
        productService.updateById(product);
        return "redirect:/back//product?pn=" + pn;
    }
}
